/**
 * 版权所有 2009-2015 中国中医科学院中医药信息研究所 何前锋 frontpeak@yahoo.com.cn
 **/
package cn.ac.cintcm.sh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.ac.cintcm.sh.dao.AliasDao;
import cn.ac.cintcm.sh.dao.HerbDao;
import cn.ac.cintcm.sh.dao.PrescriptionHerbDao;
import cn.ac.cintcm.sh.domain.Alia;
import cn.ac.cintcm.sh.domain.HerbEntry;
import cn.ac.cintcm.sh.domain.PresHerb;

/**
 * 正异名转换的服务
 * @author 何前锋 Franklin He(franklinheqf@gmail.com)
 *
 */
public class AliasService {
    
    private static HashMap<String, String> words;
    private static HashMap<String, List<String>> aliasHash;
    
    private static void initwords() {
        if (words == null) {
            words = new HashMap<String, String>();
            AliasDao aliasDao = new AliasDao();
            List<Alia> alias = aliasDao.getAlias();
            for(Alia a : alias){
                String zhengming = a.getFormalName();
                if (zhengming != null && !zhengming.trim().equals("")) {
                    String name = words.get(zhengming);
                    if (name != null) {
                        System.out.println("正名异常：" + zhengming + " 既是正名也是 " + name + " 的异名");
                    } else {
                        words.put(zhengming, zhengming);
                    }
                    String[] strs = a.getAlias();
                    if (strs != null ) {
                        for (int i = 0 ; i < strs.length; i ++) {
                            if (strs[i] != null) {
                                String str = ((String) strs[i]).trim();
                                if ( !str.equals("")) {
                                    name = words.get(str);
                                    if (name != null) {
                                        System.out.println("异名异常：" + str + " 既是 " + zhengming + "的异名也是 " + name + " 的异名");
                                    } else {
                                        words.put(str, zhengming);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    /**
     * 获取所有中药名称的正名
     * @param herbList 中药名称列表
     * @return 转换异名为正名的中药名称列表
     */
    public static List<String> getFormal(String[] herbList) {
        initwords();
        List<String> formalList = null;
        if (herbList != null) {
            formalList = new ArrayList<String>();
            for (String herb : herbList) {
                String word = getFormal(herb);
                if (word != null) {
                    formalList.add(word);
                } else {
                    herb = herb.trim();
                    formalList.add(herb);
                }
            }
        }
        return formalList;
    }

    /**
     * 获取单个中药的正名
     * @param herb 单个中药的名称
     * @return 中药的正名
     */
    public static String getFormal(String herb) {
        initwords();
        return words.get(herb);

    }
    
    /**
     * 获取中药的异名
     * @param herb 中药的名称
     * @return 中药的所有异名，如果没有异名，返回本身。
     */
    public static List<String> getInformal(String herb) {
        initwords();
        initAlias();
        return aliasHash.get(herb);
    }
    
    /**
     * 
     *
     */
    private static void initAlias() {
        if (aliasHash == null) {
            aliasHash = new HashMap<String, List<String>>();
            for (String s : words.values()) {
                List<String> as = aliasHash.get(s);
                if ( as == null) {
                    as = new ArrayList<String>();
                    aliasHash.put(s, as);
                }
            }
            for (String s : words.keySet()) {
                String a = words.get(s);
                List<String> as = aliasHash.get(a);
                as.add(s);
                aliasHash.put(s, as);
            }
        }
        
    }

    /**
     * 将中药与方剂对应关系的关系表中的中药异名与方剂对应的所有异名的id替换为其对应的正名id
     *
     */
    public static void replaceDbAlia() {
        initwords();
        HerbDao hd = new HerbDao();
        List<HerbEntry> hes = hd.getHerbs();
        HashMap<String, Integer> hbids = new HashMap<String, Integer>();
        HashMap<Integer, String> idNames = new HashMap<Integer, String>();
        for (HerbEntry he : hes) {
            hbids.put(he.getName(), he.getId());
            idNames.put(he.getId(), he.getName());
        }
        PrescriptionHerbDao phd = new PrescriptionHerbDao();
        List<PresHerb> phs = phd.getPresHerbs();
        if (phs != null) {
            for (PresHerb ph : phs) {
                int hid = ph.getId();
                String hname = idNames.get(hid);
                if (hname != null) {
                    String formalName = words.get(hname);
                    if (formalName != null) {
                        Integer fid = hbids.get(formalName);
                        if (fid != null) {
                            ph.setId(fid);
                        }
                    }
                }
            }
            phd.newPrescriptionHerbRelation(phs);
        }
        
    }
    
    public static void main(String[] args) {
        replaceDbAlia();
    }

}